//
//  BuyCouponViewController.m
//  1243
//
//  Created by 洛水寒 on 16/5/24.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "BuyCouponViewController.h"
#import "NetWorkTools.h"
@interface BuyCouponViewController ()<UITextFieldDelegate>
{
    NSString *str;
}
@property (weak, nonatomic) IBOutlet UITextField *buyNum;//购买数量
@property (weak, nonatomic) IBOutlet UILabel *totalMoney;//总计金额
@property (weak, nonatomic) IBOutlet UILabel *presentPrice;//当前价格

@property (weak, nonatomic) IBOutlet UIImageView *chooseMoney;//选择金额支付
@property (weak, nonatomic) IBOutlet UIImageView *chooseBonus;//选择积分支付
@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet UILabel *JinEMoney;
@property (weak, nonatomic) IBOutlet UILabel *JiFen;

@end


@implementation BuyCouponViewController
{
    NSNotificationCenter *_nc;//通知中心
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textField.delegate  = self;
    self.title = @"收藏企业券";
    str = @"1";
    
    //网络请求
    [self data];
    [self getMarketPrice];
    
    
    _nc = [NSNotificationCenter defaultCenter];
    //注册观察者 通知的名字一致
    [_nc addObserver:self selector:@selector(buyNumDidEndEdit) name:UITextFieldTextDidChangeNotification object:nil];
}

#pragma mark === 通知方法 ===
- (void)buyNumDidEndEdit{
    if (![_buyNum.text isEqualToString:@""]) {
        NSString *totalPrice = [NSString stringWithFormat:@"%.2f", [self.textField.text integerValue] * [_presentPrice.text floatValue]];
        self.totalMoney.text = totalPrice;
    }
}

#pragma mark === 网络请求 ===
- (void)getMarketPrice{
    NSDictionary *params = @{
                             };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params AndMenthodName:@"getPriceAndQty" completeBlock:^(id object) {
   
        NSString *string = object[@"BackData"];
        NSArray *arr = [NetWorkTools arrayWithJsonString:string];
        
        if (arr == nil) {
            _presentPrice.text = @"";
        }else{
            _presentPrice.text = [NSString stringWithFormat:@"%@", arr[0][@"MarketPrice"]];
        }
    }];
}

- (void)data{
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    NSString *userNumber = [dict objectForKey:@"Number"];
    NSString *UserName = [dict objectForKey:@"UserName"];
    
    NSDictionary *params2 = @{
                              //
                              @"Id":userNumber,
                              @"UserName":UserName
                              };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params2 AndMenthodName:@"getUserInfo" completeBlock:^(id object) {
        
        
        NSString *string = object[@"BackData"];
        NSArray *arr = [NetWorkTools arrayWithJsonString:string];
        //        NSLog(@"用户信息%@",object);
        if (arr == nil) {
            self.JinEMoney.text = @"";
            self.JiFen.text = @"";
        }else if ([arr[0][@"Money"] isEqual:[NSNull null]]){
            self.JinEMoney.text = @"";
        }else if ([arr[0][@"fh_Money"] isEqual:[NSNull null]]){
            self.JiFen.text = @"";
        }
        else{
            self.JinEMoney.text =  [NSString stringWithFormat:@"%.2f",[arr[0][@"Money"] floatValue]];
            self.JiFen.text =  [NSString stringWithFormat:@"%.2f",[arr[0][@"fh_Money"] floatValue]];
        }
    }];
}

- (void)getUserData:(NSString *)strA{
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    NSString *userNumber = [dict objectForKey:@"Number"];
    NSString *PayMoney = _totalMoney.text;
    NSDictionary *params2 = @{
                              //
                              @"TradeUserId":userNumber,
                              @"TradeQty":self.buyNum.text, //数量
                              @"TradePrice":@"10",            //交易价格
                              @"PayMoney":PayMoney ,//交易金额
                              @"PayType":strA // 交易方式
                              };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params2 AndMenthodName:@"BuyCompanyTicket" completeBlock:^(id object) {
        if ([[NSString stringWithFormat:@"%@",object[@"Id"]] isEqualToString:@"0"]) {
            self.buyNum.text = @"";
            self.totalMoney.text = @"";
            
        }
        [self getMarketPrice];
        [self data];
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alview show];
        
     
        
    }];
}

//金额支付
- (IBAction)payUseMoney:(id)sender {
    _chooseMoney.backgroundColor = [UIColor lightGrayColor];
    _chooseBonus.backgroundColor = [UIColor whiteColor];
    str = @"1";
}
//积分支付
- (IBAction)payUseBonus:(id)sender {
    _chooseBonus.backgroundColor = [UIColor lightGrayColor];
    _chooseMoney.backgroundColor = [UIColor whiteColor];
    str = @"2";
}

- (IBAction)BuyBuyBuy:(id)sender {
    [self getUserData:str];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_buyNum resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
